//
//  WorkingView.swift
//  xgwjinterconnection
//  //运行信息页面
//  Created by WangWeiShuo on 2025/8/18.
//

import SwiftUI
import Combine

struct WorkingView: View {
    @Environment(\.dismiss) var dismiss
    @StateObject private var socketManager = SocketManager.shared
    @State private var cancellables = Set<AnyCancellable>()
    // 用于存储定时器订阅
    @State private var cancellable: AnyCancellable?
    // 控制定时器状态
    @State private var isRunning: Bool = false
    
    @State private var workingInfo: WorkingInfoBean?
    
    var body: some View {
        ZStack(alignment: .bottom){
            VStack(){
                WikaNavigationView(showBackIcon: true, title: NSLocalizedString("working_info", comment: "运行信息"),onBack: {
                    dismiss() }).frame(maxHeight: 65)
                
                FunctionWorkingView(workName: NSLocalizedString("working_gear", comment: "油门档位"), workValue: workingInfo?.gear ?? "N0", workUnit: "",backGroundColor: "color_gray_f3f3f3").padding(.top,-25)
                
                FunctionWorkingView(workName: NSLocalizedString("working_enginespeed", comment: "发动机转速"), workValue: workingInfo?.enginespeed ?? "1000", workUnit: "rpm",backGroundColor: "color_white")
                
                FunctionWorkingView(workName: NSLocalizedString("working_enginetemp", comment: "发动机水温"), workValue: workingInfo?.enginetemp ?? "50", workUnit: "°C",backGroundColor: "color_gray_f3f3f3")
                
                FunctionWorkingView(workName: NSLocalizedString("working_fuellevel", comment: "燃油液位"), workValue: workingInfo?.fuellevel ?? "5", workUnit: "%",backGroundColor: "color_white")
                
                FunctionWorkingView(workName: NSLocalizedString("working_systemvoltage", comment: "系统电压"), workValue: workingInfo?.systemvoltage ?? "24", workUnit: "V",backGroundColor: "color_gray_f3f3f3")
                
                FunctionWorkingView(workName: NSLocalizedString("working_urealevel", comment: "尿素液位"), workValue: workingInfo?.urealevel ?? "N9", workUnit: "%",backGroundColor: "color_white")
                
                FunctionWorkingView(workName: NSLocalizedString("working_oilpressure", comment: "机油压力"), workValue: workingInfo?.oilpressure ?? "20", workUnit: "kPa",backGroundColor: "color_gray_f3f3f3")
                
                
                Spacer()
            }
            .navigationTitle("")
            .navigationBarHidden(true)
        }
        .enableSwipeBackGesture()
        .onAppear {
            // 订阅用户信息消息
            socketManager.subscribeToWorkingInfo()
                .receive(on: DispatchQueue.main)
                .sink {  info in
                    workingInfo = info
                }
                .store(in: &cancellables)
            SendMsgUtils.sendGetMessage(socket: socketManager,type: SendMessageType.GET_MSG_OPREATION_INFO)
            startTimer()
        }
        .onDisappear{
            stopTimer()
        }
    }
    
    // 启动定时器
    private func startTimer() {
        isRunning = true
        // 创建定时器：每隔1秒发送一次信号，在主线程接收
        cancellable = Timer.publish(every: 5, on: .main, in: .common)
            .autoconnect()
            .sink { _ in
                // 每秒执行的任务
                print("定时器执行")
                SendMsgUtils.sendGetMessage(socket: socketManager,type: SendMessageType.GET_MSG_OPREATION_INFO)
            }
    }
    
    // 停止定时器
    private func stopTimer() {
        isRunning = false
        cancellable?.cancel() // 取消订阅
        cancellable = nil
    }
}

struct FunctionWorkingView: View {
    let workName: String
    let workValue: String
    let workUnit: String
    let backGroundColor: String
    
    var body: some View {
        HStack {
            Text(workName).foregroundColor(.black)
            Spacer()
            Text(workValue).foregroundColor(.black)
            Text(workUnit).foregroundColor(.black)
        }
        .padding()
        .background(Color(backGroundColor))
        .cornerRadius(8)
        .shadow(color: Color.gray.opacity(0.2), radius: 4, x: 0, y: 2)
        .padding(.horizontal, 16)
    }
}


#Preview {
    WorkingView()
}
